<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .min {
            position: relative;
            width: 400px;
            height: 400px;
        }

        .min img {
            width: 400px;
            height: 300px;
        }

        .mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
            background-color: #FEDE4F;
            /* 半透明 */
            opacity: .5;
            border: 1px solid #ccc;
            cursor: move;
            display: none;
        }

        .big {
            position: absolute;
            top: 0px;
            left: 410px;
            width: 500px;
            height: 500px;
            display: none;
            z-index: 3;
            border: 1px solid #ccc;
            overflow: hidden;
        }

        .big img {
            position: absolute;
            top: 0;
            left: 0;
            width: 1100px;
            height: 900px;
        }
    </style>
</head>

<body>
    <div class="min">
        <img src="8.jpg" alt="">
        <div class="mask"></div>
        <div class="big">
            <img src="8.jpg" alt="" class="bigimg">
        </div>
    </div>

</body>
<script>
    // 页面加载完毕后 执行里面的代码
    window.addEventListener('load', function () {
        var min = document.querySelector('.min')
        var mask = document.querySelector('.mask')
        var big = document.querySelector('.big')
        // 鼠标移入 mask 和big 显示
        min.addEventListener('mouseover', function () {
            mask.style.display = 'block';
            big.style.display = 'block';
        })
        // 鼠标移出 mask 和big 隐藏
        min.addEventListener('mouseout', function () {
            mask.style.display = 'none';
            big.style.display = 'none';
        })
        // 鼠标在min 内移动  mask跟着移动
        min.addEventListener('mousemove', function (e) {
            // 计算出鼠标在盒子内的坐标
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            // mask 移动的距离
            var maskx = x - mask.offsetWidth / 2;
            var masky = y - mask.offsetHeight / 2;
            // 不让mask 移出 min盒子 加入判断
            var maskmax = min.offsetWidth - mask.offsetWidth;
            var maskmaxy = min.offsetHeight - mask.offsetHeight - 100
            if (maskx <= 0) {
                maskx = 0
            } else if (maskx >= maskmax) {
                maskx = maskmax;
            }
            if (masky <= 0) {
                masky = 0
            } else if (masky >= maskmaxy) {
                masky = maskmaxy;
            }
            mask.style.left = maskx + 'px';
            mask.style.top = masky + 'px';
            // 大图    移动距离 = 遮挡层移动距离 * 大图片的最大移动距离 / 遮挡层的最大移动距离
            var bigimg = document.querySelector('.bigimg')
            // 大图的最大移动距离 x y 
            var bigmax = bigimg.offsetWidth - big.offsetWidth
            var bigmaxy = bigimg.offsetHeight - big.offsetHeight
            // 大图片的移动距离 x y
            var bigx = maskx * bigmax / maskmax;
            var bigy = masky * bigmaxy / maskmaxy;
            bigimg.style.top = -bigy + 'px';
            bigimg.style.left = -bigx + 'px';
        })
    })
</script>

</html>